package com.tianpengtech.admin.authenticate;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Model;
import com.tianpengtech.common.exception.ServiceException;
import com.tianpengtech.common.exception.ValidateException;
import com.tianpengtech.common.im.kit.PushKit;
import com.tianpengtech.admin.AbstractAdmin;
import com.tianpengtech.admin.IAdminOpt;
import com.tianpengtech.admin.opertion.OperationDao;
import com.tianpengtech.common.model.account.Account;
import com.tianpengtech.common.model.authenticate.Authenticate;
import com.tianpengtech.common.model.fair.Fair;
import com.tianpengtech.common.model.opertion.Opertion;
import com.tianpengtech.common.search.EqualCondition;
import com.tianpengtech.common.search.LikeCondition;
import com.tianpengtech.common.search.QueryConstructor;
import com.tianpengtech.common.util.XxxUtil;
import com.tianpengtech.common.validator.Validator;
import com.tianpengtech.common.validator.rules.numberic.NumbericMustBeGreaterThan;
import com.tianpengtech.common.validator.rules.string.StringLengthBetween;

public class AuthenticateAdminServiceImpl extends AbstractAdmin implements IAdminOpt{

	@Override
	public void beforeAdd(Model<?> model) throws ValidateException {

	}

	@Override
	public void beforeQueryById(Model<?> model, int id) {

	}

	@Override
	public void beforeUpdate(Model<?> model) throws ValidateException {

	}

	@Override
	public void beforeDelete(Model<?> model) {

	}

	@Override
	public void builderSQLExpected(Model< ? > model,
			QueryConstructor queryConstruct, Map< String, Object > params) {

		int status = model.getInt(Authenticate.STATUS);
		String companyName = model.getStr(Authenticate.COMPANY_NAME);
		int BLANK =-1;
		queryConstruct
			.and(new EqualCondition(Authenticate.DELETETAG, Authenticate.DELETE_STATUS_NO));
		if(StringUtils.isNotEmpty(companyName)){
			queryConstruct
			.and(new LikeCondition(Authenticate.COMPANY_NAME,"%"+companyName+"%"));
		}
		if(status>BLANK){
			queryConstruct
			.and(new EqualCondition(Authenticate.STATUS,status));
		}
	}

	@Override
	public String tableName() {
		return Authenticate.dao.tableName();
	}
	
	
	
	@Override
	public boolean deleteById(Model<?> model, int id) throws ValidateException, ServiceException {
		model.set(Authenticate.DELETETAG, Authenticate
				.DELETE_STATUS_YES);
		return model.update();
	}

	@Override
	public void changeStatus(Model<?> model, long id, int status, Kv params) throws ValidateException {

		String content = params.getStr(Opertion.CONTENT);
		Validator validator = new Validator();
		if(status==Authenticate.STATUS_FAILURE){
			validator.addRule( new StringLengthBetween(content,"拒绝理由必须是1-40个字符之间",1,40));
		}
		validator.addRule( new NumbericMustBeGreaterThan(id,0,"id值必须要大于0")).excute();
		
		
		model.set(Authenticate.ID, id);
		model.set(Authenticate.STATUS, status);
		model.update();
		
		Model<Authenticate> temp = Authenticate.dao.findById(id);
		Account account = XxxUtil.readSessionInfo();
		long userId = account.getLong(Account.ID);
		if(status==Authenticate.STATUS_SUCCESS ){
			String result = "【通过】"+temp.getStr(Authenticate.COMPANY_NAME)+"的认证请求";
			PushKit.pushSystemMessageNow("管理员"+result);
			OperationDao.ME.recordLog(id, Opertion.OPT_AUTHENTICATE_LOG,userId,result);
		}
		if(status==Authenticate.STATUS_FAILURE){
			
			String result = "【拒绝】"+temp.getStr(Authenticate.COMPANY_NAME)+"的认证请求,原因是:"+content;
			PushKit.pushSystemMessageNow("管理员"+result);
			OperationDao.ME.recordLog(id, Opertion.OPT_AUTHENTICATE_LOG,userId,result);
		}
	}

}